//
//  AppDelegate.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/8/12.
//  Copyright (c) 2015年 NewEggTech. All rights reserved.
//

#import "AppDelegate.h"
#import "AVOSCloudSNS.h"
#import <AVOSCloud/AVOSCloud.h>
#import "AVOSManger.h"
#import "TLLaunchAniController.h"
#import "TLItdManger.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import "WXApi.h"

@interface AppDelegate ()

@end

@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
   [[UITabBar appearance] setTintColor:[UIColor colorWithRed:227/255.f green:98/255.f blue:94/255.f alpha:1.f]];
    
    [[UINavigationBar appearance] setTintColor:AdornColor];
    
    //Set the Application Window
    self.window = [AppDelegate sharedLaunchAniWindow];
    //Set the Animation ViewController
    self.window.rootViewController = [AppDelegate animationViewController];
    
    [[AppDelegate animationViewController] startAnimation];
    
    //Check needed for show Introduction Controller
    if([TLItdManger needShowIntroductionController])
    {
        [[TLItdManger sharedManger] showItdViewController:[AppDelegate animationViewController] completionBlock:^{
            [AppDelegate changeRootViewController];
        }];
    }
  //  else
//        [AppDelegate changeRootViewController];
     
   // [[AppDelegate animationViewController] startAnimation];
    
    
    
    // Override point for customization after application launch.
    [AVOSCloud setApplicationId:@"BTyPio5OOqGvDADGcQfeFsdb" clientKey:@"8OGT8rEuBtc2UaOkaOf3PHrS"];
    [AVAnalytics setChannel:@"App store"];
    [AVAnalytics setAnalyticsEnabled:YES];
    
//  [[AVUser currentUser]];
    /*
    [[AVOSManger sharedManger] queryVideoIdBySHA:@"BTyPio5OOqGvDADGcQfeFsdb" completion:^(NSString *videoId) {
        [[AVOSManger sharedManger] sendComment:@"Testttttt" videoId:videoId userSession:@"ajvu1cu3l2svbvz67sbi1eevz" completion:^(NSString *resp) {
            NSLog(@"response %@",resp);
        }];
    }];
     
    [[AVOSManger sharedManger] queryVideoIdBySHA:@"BTyPio5OOqGvDADGcQfeFsdb" completion:^(NSString *videoId) {
        [[AVOSManger sharedManger] getCommentWithVideoId:videoId completion:^(NSUInteger httpcode, NSDictionary *respDic) {
            NSLog(@"response httpcode:%ld response %@",httpcode,respDic);
        }];
    }];*/

    //设定全局Bar tint
    //[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
    
    //self.window.rootViewController = [AppDelegate animationViewController];
    
    //[[AppDelegate animationViewController] startAnimation];
    
    self.window.layer.contents = (id)[UIImage imageNamed:@"Launch"].CGImage;
    
    [self.window makeKeyAndVisible];
    
    //启动社交分享
    [self setupSNS];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

/**
 *  Fetch Launch Animation Window
 *
 *  @return The Application Window
 */
+ (UIWindow *)sharedLaunchAniWindow{
    static dispatch_once_t onceToken;
    static UIWindow        *aniWindow;
    dispatch_once(&onceToken, ^{
        aniWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [TLDeviceManger deviceScreenSize].width, [TLDeviceManger deviceScreenSize].height)];
    });
    
    return aniWindow;
}

//The Animation
+ (TLLaunchAniController *)animationViewController{
    static dispatch_once_t onceToken;
    static TLLaunchAniController *aniViewController;
    dispatch_once(&onceToken, ^{
        aniViewController = [[TLLaunchAniController alloc] initWithGifPath:[[NSBundle mainBundle] pathForResource:LaunchAniFileNoneExt ofType:@"gif"]];
    });
    
    return aniViewController;
}

+ (UITabBarController *)tabbarController{
    UIStoryboard *mainSb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    
    UITabBarController *appRootController = [mainSb instantiateViewControllerWithIdentifier:@"EntryPoint"];
    
    return appRootController;
}


+ (void)changeRootViewController{
    UIStoryboard *mainSb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    
    UIViewController *appRootController = [mainSb instantiateViewControllerWithIdentifier:@"EntryPoint"];
    
    CGSize ogSize = [AppDelegate sharedLaunchAniWindow].frame.size;
    
    [[AppDelegate sharedLaunchAniWindow] setFrame:CGRectMake(0, 0, ogSize.width, ogSize.height)];
    
    
    [[AppDelegate sharedLaunchAniWindow] setRootViewController:appRootController];
    
    [[AppDelegate sharedLaunchAniWindow] makeKeyAndVisible];
}

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    return [AVOSCloudSNS handleOpenURL:url];
}

// When Build with IOS 9 SDK
// For application on system below ios 9
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [AVOSCloudSNS handleOpenURL:url];
}
// For application on system equals or larger ios 9
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    return [AVOSCloudSNS handleOpenURL:url];
}

- (void)setupSNS{
    
    
    
    [ShareSDK registerApp:@"e26701d32a40"
     
          activePlatforms:@[

                            @(SSDKPlatformTypeWechat)
        ]
                 onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeWechat:
                 [ShareSDKConnector connectWeChat:[WXApi class]];
                 break;
             default:
                 break;
         }
     }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         
         switch (platformType)
         {
            case SSDKPlatformTypeWechat:
                 [appInfo SSDKSetupWeChatByAppId:@"wx78c41f313e22f305"
                                       appSecret:@"54f583ff8941055f4514ac9bbb4439e5"];
                 break;
             default:
                 break;
         }
     }];
    
}



@end
